/**
 * Generali Solutions d'assurances - Tous droits réservés &copy; 2007 - 2010
 */
package fr.generali.accueilclient.utils.oxm;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.XmlMappingException;

/**
 * @author Holmes Kelly
 */
public class OxMapperImpl implements OxMapper {

    /**
     * LOGGER
     */
    private static final Logger LOG = LoggerFactory.getLogger(OxMapperImpl.class);

    /**
     * le marshaller
     */
    private Marshaller marshaller;

    /**
     * le unmarshaller
     */
    private Unmarshaller unmarshaller;

    /**
     * {@inheritDoc}
     */
    public void writeObjectToXml(Object object, String filename) throws IOException {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(filename);
            marshaller.marshal(object, new StreamResult(fos));
        } catch (XmlMappingException e) {
            LOG.error("Xml-Serialization failed due to an XmlMappingException.", e);
        } catch (IOException e) {
            LOG.error("Xml-Serialization failed due to an IOException.", e);
        } finally {
            if (fos != null) {
                fos.close();
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    public void writeObjectToXml(Object object, StringWriter writer) {

        try {
            marshaller.marshal(object, new StreamResult(writer));
        } catch (XmlMappingException e) {
            LOG.error("Xml-Serialization failed due to an XmlMappingException.", e);
        } catch (IOException e) {
            LOG.error("Xml-Serialization failed due to an IOException.", e);
        }
    }

    /**
     * {@inheritDoc}
     */
    public Object readObjectFromXml(String filename) throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filename);
            return unmarshaller.unmarshal(new StreamSource(fis));
        } catch (IOException e) {
            LOG.error("Xml-Deserialization failed due to an IOException.", e);
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public Object readObjectFromXml(StringReader reader) {

        try {
            return unmarshaller.unmarshal(new StreamSource(reader));
        } catch (XmlMappingException e) {
            LOG.error("Xml-Serialization failed due to an XmlMappingException.", e);
        } catch (IOException e) {
            LOG.error("Xml-Deserialization failed due to an IOException.", e);
        }

        return null;
    }

    /**
     * @return the marshaller
     */
    public Marshaller getMarshaller() {
        return marshaller;
    }

    /**
     * @param marshaller the marshaller to set
     */
    public void setMarshaller(Marshaller marshaller) {
        this.marshaller = marshaller;
    }

    /**
     * @return the unmarshaller
     */
    public Unmarshaller getUnmarshaller() {
        return unmarshaller;
    }

    /**
     * @param unmarshaller the unmarshaller to set
     */
    public void setUnmarshaller(Unmarshaller unmarshaller) {
        this.unmarshaller = unmarshaller;
    }

}
